#include "channelMap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//初始化
struct ChannelMap* channelMapInit(int size)
{
    struct ChannelMap* map = (struct ChannelMap*)malloc(sizeof(struct ChannelMap));
    map->size = size;
    map->list = (struct Channel**)malloc(sizeof(struct Channel*) * size);
    return map;
}

//清空map
void ChannelMapClaer(struct ChannelMap* map)
{
    if(map != NULL)
    {
        for(int idx = 0; idx < map->size; idx++)
        {
            if(map->list[idx] != NULL)
            {
                free(map->list[idx]);
            }
        }
        free(map->list);
        map->list = NULL;
    }
    map->size = 0; 
}

//扩容
bool makeMapRoom(struct ChannelMap* map, int newSize, int unitSize)
{
    if(map->size < newSize)
    {
        int curSize = map->size;
        //容量每次扩大一倍
        while(curSize < newSize)
        {
            curSize *= 2;
        }
        //扩容realloc
        struct Channel** newPtr = realloc(map->list, curSize * unitSize);
        if(newPtr == NULL)
        {
            return false;
        }
        map->list = newPtr;
        memset(&(map->list[map->size]), 0, (curSize - map->size) * unitSize);
        map->size = curSize;
    }
    return true;
}